<?php

namespace App\Repositories;

use Carbon\Carbon;
use App\Exceptions\BasicException;

class AuctionRepository
{

    public function getList($model)
    {
        return app($model)->orderBy('sort', 'asc')->orderBy('id', 'desc')->get();
    }

    public function add($model, $data)
    {
        app($model)->insert($data);
    }

    public function update($model, $id, $data)
    {
        app($model)->where('id', $id)->update($data);
    }

    public function delete($model, $id)
    {
        $detail = app($model)->where('id', $id)->first();
        if (!$detail) {
            throw new BasicException(0, '数据不存在');
        }
        app($model)->where('id', $id)->delete();
    }

    public function getRecord($param, $page, $perPage)
    {
        $list = app('model_auction_record')
            ->with('user_data:user_id,head_picture,user_code')
            ->with('user_room:id,room_id')
            ->with([
                'auction' => function ($query) {
                    $query->select([
                        'id',
                        'auction_gift_id',
                        'content',
                    ])->with('gift:id,gift_price,gift_picture,gift_name');
                },
            ])
            ->orderBy('id', 'desc')
            ->paginate($perPage, ['*'], '', $page);
        $list->getCollection()->transform(function ($item) {
            return collect([
                'user_code'    => $item->user_data->user_code ?? '',
                'head_picture' => $item->user_data->head_picture ?? '',
                'room_code'    => $item->user_room->room_id ?? '',
                'room_name'    => $item->user_room->room_name ?? '',
                'content'      => $item->auction->content ?? '',
                'gift_price'   => $item->auction->gift->gift_price ?? 0,
                'gift_picture' => $item->auction->gift->gift_picture ?? '',
                'current_cost' => $item->current_cost ?? 0,
                'total_cost'   => $item->total_cost ?? 0,
                'number'       => $item->number ?? 0,
                'add_time'     => date('Y-m-d H:i:s', $item->add_time),
                'is_final'     => $item->is_final ?? 0,
            ]);
        });

        return $list;
    }

    /**
     * 获取中奖记录信息
     * @param $param array 参数集合
     * @param $page int 页码
     * @param $perPage int 分页条码
     * @return mixed
     */
    public function getLog($param, $page, $perPage)
    {
        $query = app('model_auction_log')
            ->with('auction_user_data:user_id,head_picture,user_code,nickname')
            ->with('participation_user_data:user_id,head_picture,user_code,nickname')
            ->with('room:id,room_name,room_id');
        //筛选房间id/房间昵称
//        if (!empty($param['room_info'])) {
//            $query = $query->where(function ($query) use ($param) {
//                $query->where('room_id', 'like', '%' . $param['room_info'] . '%')
//                    ->orWhere('room_name', 'like', '%' . $param['room_info'] . '%');
//            });
//        }
        if (!empty($param['room_info'])) {
            $query = $query->whereHas('room', function ($query) use ($param) {
                $query->where('room_id', 'like', '%' . $param['room_info'] . '%')
                    ->orWhere('room_name', 'like', '%' . $param['room_info'] . '%');
            });
        }
        //筛选拍卖嘉宾用户code/昵称
        if (!empty($param['auction_user_info'])) {
            $query = $query->whereHas('auction_user_data', function ($query) use ($param) {
                $query->where('nickname', 'like', '%' . $param['auction_user_info'] . '%')
                    ->orWhere('user_code', '=', $param['auction_user_info']);
            });
        }
        //筛选竞拍用户code/昵称
        if (!empty($param['participation_user_info'])) {
            $query = $query->whereHas('participation_user_data', function ($query) use ($param) {
                $query->where('nickname', 'like', '%' . $param['participation_user_info'] . '%')
                    ->orWhere('user_code', '=', $param['participation_user_info']);
            });
            $query->where('participation_user_id','>',0);
        }
        // 时间
        if (!empty($param['start_time'])) {
            $query->where('create_time', '>=', date('Y-m-d H:i:s',$param['start_time']));
        }
        if (!empty($param['end_time'])) {
            $query->where('end_time', '>=', date('Y-m-d H:i:s',$param['end_time']));
        }
        if (!empty($param['auction_result'])) {
            $query->where('auction_result', '=', $param['auction_result']);
        }


        $list = $query->whereIn('auction_result', [1, 2])
            ->select(
                'id',
                'room_id',
                'room_name',
                'auction_user_id',
                'auction_user_name',
                'participation_user_id',
                'participation_user_name',
                'auction_content',
                'auction_content_timeliness',
                'auction_gift_name',
                'auction_result_number',
                'auction_result',
                'end_time'
            )
            ->orderBy('id', 'desc')
            ->paginate($perPage, ['*'], '', $page);


        $list->getCollection()->transform(function ($item) {
            return collect([
                'id'                         => $item->id ?? '',
                'room_id'                    => $item->room->room_id ?? '',
                'room_name'                  => $item->room_name ?? '',
                'auction_user_code'          => $item->auction_user_data->user_code ?? '',
                'auction_user_name'          => $item->auction_user_data->nickname ?? '',
                'participation_user_code'    => $item->participation_user_data->user_code ?? '',
                'participation_user_name'    => $item->participation_user_data->nickname ?? '',
                'auction_content'            => $item->auction_content ?? '',
                'auction_content_timeliness' => $item->auction_content_timeliness ?? '',
                'auction_gift_name'          => $item->auction_gift_name ?? '',
                'auction_result_number'      => $item->auction_result_number ?? '',
                'auction_result'             => $item->auction_result ?? 0,
                'end_time'                   => $item->end_time,
            ]);
        });

        // print_r($list['data']);
        // exit;

        return $list;
    }

    /**
     * 获取拍卖详情
     * @param $id
     */
    public function getAuctionDetail($id)
    {
        $res = app('model_auction_log')
            ->with('auction_user_data:user_id,head_picture,user_code,nickname')
            ->with('participation_user_data:user_id,head_picture,user_code,nickname')
            ->with('room:id,room_name,room_id')
            ->where('id',$id)
            ->first();
        return empty($res) ? [] : $res->toarray();
    }

    /**
     * 获取内容列表
     */
    public function getContentList($page, $perPage)
    {
        return app('model_auction_content')
            ->orderBy('sort', 'asc')
            ->orderBy('id', 'desc')
            ->paginate($perPage, ['*'], '', $page);
    }
}
